/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import datamodel.SystemAdmin;
import datamodel.SystemCustomer;
import datamodel.SystemUser;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author eXtreme
 */
@Stateless
public class UserManager {

    public static final String ROLE_ADMIN = "SystemAdmin";
    public static final String ROLE_CUSTOMER = "SystemCustomer";
    
    @PersistenceContext
    private EntityManager em;

    public List<SystemUser> getAllUsers() {
        return em.createQuery("SELECT u FROM SystemUser u").getResultList();
    }
    
    public SystemUser getUser(long id) {
        return em.find(SystemUser.class, id);
    } 
    
    public SystemCustomer getCustomer(long id) {
        return em.find(SystemCustomer.class, id);
    }
    
    public SystemAdmin getAdmin(long id) {
        return em.find(SystemAdmin.class, id);
    }
    
    public List<String> getUserRoleTypes() {
        List<String> l = new ArrayList<String>(2);
        l.add(ROLE_ADMIN);
        l.add(ROLE_CUSTOMER);
        return l;
    }
    
    // UNUSED ?
    public SystemUser getUserByData(String usermail, String password) {
        Query query = em.createQuery("SELECT u FROM SystemUser u WHERE u.email = :usermail AND u.password = :password");
        query.setParameter("usermail", usermail);
        query.setParameter("password", password);

        try {
            return (SystemUser) query.getSingleResult();
        } catch (NoResultException ex) {
            System.out.println("CHYBA: " + ex);
            return null;
        }
    }
    
    public SystemUser getUserByName(String usermail) {
        Query query = em.createQuery("SELECT u FROM SystemUser u WHERE u.email = :usermail");
        query.setParameter("usermail", usermail);

        try {
            return (SystemUser) query.getSingleResult();
        } catch (NoResultException ex) {
            System.out.println("CHYBA: " + ex);
            return null;
        }
    }

    public void saveUser(SystemUser temp) {
        em.persist(temp);
        em.flush();
    }

    public boolean exists(String usermail) {
        Query query = em.createQuery("SELECT u.email FROM SystemUser u WHERE u.email = :usermail");
        query.setParameter("usermail", usermail);

        try {
            if (query.getSingleResult() != null) {
                return true;
            }
            return false;
        } catch (NoResultException ex) {
            return false;
        }
    }

    @RolesAllowed({"SystemAdmin"})
    public void remove(SystemUser temp) {
        em.remove(temp);
        em.flush();
    }
}
